<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/14 0014
 * Time: 9:34
 */

//添加接口请求日志
function add_interface_request_log($params, $action, $member_ip = '', $param) {
    if(!$member_ip) return ['data'=>'add_interface_request_log','code'=>400001,'msg'=> 'FAULT'];
    //$log['app_id'] = $param['app_id'];
    $log['interface'] = $action;
    $log['request_time'] = time();
    $log['member_ip'] = $member_ip;
    $log['data'] = json_encode($param);
    if(isset($params['member_id'])) $log['member_id'] = $params['member_id'];
    $rs = db('interface_request_log')->insert($log);
    if(!$rs)
        return ['data'=>'','code'=>400067,'msg'=> 'FAULT'];
    else
        return ['data'=>'','code'=>200,'msg'=> 'SUCCESS'];
}


// 接口身份验证
function check_merchant($params) {
    if (!isset($params['sign']) || !$params['sign']) {
        return ['data'=>'','code'=>400110,'message'=>'FAULT'];
    }
    $sign = strtolower($params['sign']);//pj170312 姜涛传过来的是大写的
    $timestamp = $params['timestamp'];
    $key = 'jgsdfjkfgjs#$%&khs2423hkfh';

    if((time() - $timestamp) > 600) {
        return ['data'=>'','code'=>400004,'message'=>'FAULT'];
    }

    $check_sign = md5('timestamp=' . $timestamp . '&key=' . $key);

    if($check_sign != $sign) {
        return ['data'=>'','code'=>400005,'message'=>'FAULT'];
    }
    return ['data'=>'','code'=>200,'message'=>'SUCCESS'];
}

// 上传身份证
// $data  原数据 身份证上传完成之后需要合并到原数据里面
// $action_dir 图片保存的目录
function upload_card($data, $action_dir) {
    $files = request()->file('card');
    $id_card_in_hand = $id_card_front = $id_card_verso = '';
    if($files) {
        $i = 1;
        foreach ($files as $k => $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $action_dir);
            if ($info) {
                // 输出 图片地址
                switch ($i) {
                    case 1:
                        $id_card_in_hand = $action_dir . DS . $info->getSaveName();
                        break;
                    case 2:
                        $id_card_front = $action_dir . DS . $info->getSaveName();
                        break;
                    case 3:
                        $id_card_verso = $action_dir . DS . $info->getSaveName();
                        break;
                    default:
                        break;
                }
            } else {
                // 上传失败, 返回错误信息
                //return json(['data' => '', 'code' => 400010, 'msg' => ]);
                return json(['data' => '', 'code' => 400010, 'msg' => $file->getError()]);
            }
            $i++;
        }
    }
    if(!$id_card_in_hand || !$id_card_front || !$id_card_verso) {
        return json(['data' => '', 'code' => 400070, 'msg' => '']);
    }
    $data = array_merge_recursive($data, ['id_card_in_hand' => $id_card_in_hand, 'id_card_front' => $id_card_front, 'id_card_verso' => $id_card_verso]);
    return json(['data' => $data, 'code' => 200, 'msg' => '']);;
}


// 给二维码加上logo
// $erweima_url 二维码图片地址
function qr_code_add_log($qr_code_url) {
    $logo = ROOT_PATH . 'public' . DS . 'static' . DS . 'images' . DS . 'logo.png';//准备好的logo图片
    $QR = $qr_code_url;//已经生成的原始二维码图

    if ($logo !== FALSE) {
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二维码图片宽度
        $QR_height = imagesy($QR);//二维码图片高度
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        //重新组合图片并调整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
            $logo_qr_height, $logo_width, $logo_height);
    }
//输出图片
    imagepng($QR, $qr_code_url);
}


// 添加站内信
// $members 用户id数组，全部用户传入 all
// $type 站内信类型 1：系统； 2：订单； 3：活动；
// $msg 站内信内容
// $title 站内信标题；
function create_message($members, $type, $msg, $title) {
    $data = [
        'type' => $type,
        'msg' => $msg,
        'title' => $title,
        'status' => 1,
        'created_at' => time(),
    ];

    if ($members == 'all'){
        $members = db('member')->column('id');
    } else {
        if (!is_array($members))
            return false;
    }
    $messages = [];
    foreach ($members as $id) {
        $data['member_id'] = $id;
        $messages[] = $data;
    }
    $rs = db('member_message')->insertAll($messages);
    return $rs;
}



//根据pid(上级分类ID)取出后代， 并且给每一代分类定义好级别
function get_cates($table, $pid = 0, $level = 0, $noid = 0)
{
    static $res = [];// static 定义函数内部的全局变量; 当函数内部调用该函数本身时，所得数据需要该定义才能追加保存到同一变量；
    $data = db($table)->where('pid', $pid)->where('id', 'neq', $noid)->select();
    //print_r($data);die;
    foreach($data as $v)
    {
        $v['level'] = $level;	//记录级别
        $res[] = $v;
        get_cates($table, $v['id'], $level+1, $noid);	//函数内部调用该函数本身
    }

    return $res;
}

//获取分组分类
function get_group_cates($table)
{
    //static $res = [];// static 定义函数内部的全局变量; 当函数内部调用该函数本身时，所得数据需要该定义才能追加保存到同一变量；
    $items = db($table)->select();
    //print_r($data);die;
    foreach ($items as $item)
    {
        $tmpMap[$item['id']] = $item;
    }

    // print_r($tmpMap);
    foreach ($items as $item)
    {
        if (isset($tmpMap[$item['pid']]))
        {
            $tmpMap[$item['pid']]['childrens'][] = &$tmpMap[$item['id']];
        }
        else
        {
            $tree[] = &$tmpMap[$item['id']];
        }
    }

    return $tree;
}
